package com.Service.impl;

import com.mapper.CartMapper;
import com.mapper.OrderMapper;
import com.mapper.OrderItemMapper;
import com.mapper.ProductMapper;
import com.Service.OrderService;

import com.pojo.*;

import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Override
    public List<Order> findByUserId(Long userId) {
        return orderMapper.findByUserId(userId);
    }

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Order> findAll() {
        return orderMapper.findAll();
    }
    @Override
    public void submit(Order order) {
        orderMapper.insert(order);
    }
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Override
    public List<OrderVO> findOrdersByUserId(Long userId) {
        List<Order> orders = orderMapper.findByUserId(userId);
        List<OrderVO> result = new ArrayList<>();

        for (Order order : orders) {
            OrderVO orderVO = new OrderVO();
            orderVO.setOrder(order);

            List<OrderItem> orderItems = orderItemMapper.findByOrderId(order.getId());
            List<OrderItemVO> itemVOList = new ArrayList<>();

            for (OrderItem item : orderItems) {
                Product product = productMapper.findById(item.getProductId());

                OrderItemVO itemVO = new OrderItemVO();
                itemVO.setOrderItem(item);
                itemVO.setProduct(product);

                itemVOList.add(itemVO);
            }

            orderVO.setItems(itemVOList);
            result.add(orderVO);
        }

        return result;
    }
    @Resource
    private CartMapper cartMapper;
    @Override
    public void createOrder(Long userId, Map<Long, Integer> productQuantityMap) {
// 计算总价
        BigDecimal totalPrice = BigDecimal.ZERO;
        List<OrderItem> itemList = new ArrayList<>();

        for (Map.Entry<Long, Integer> entry : productQuantityMap.entrySet()) {
            Long productId = entry.getKey();
            Integer qty = entry.getValue();
            Product product = productMapper.findById(productId);

            OrderItem item = new OrderItem();
            item.setProductId(productId);
            item.setQuantity(qty);
            item.setUnitPrice(product.getPrice());
            itemList.add(item);

            totalPrice = totalPrice.add(product.getPrice().multiply(BigDecimal.valueOf(qty)));
        }

        // 创建订单
        Order order = new Order();
        order.setUserId(userId);
        order.setStatus("待支付");
        order.setTotalPrice(totalPrice);
        order.setCreatedAt(new Date());
        orderMapper.insert(order);

        // 插入订单项
        for (OrderItem item : itemList) {
            item.setOrderId(order.getId());
            orderItemMapper.insert(item);
        }

        // 清空购物车中已结算的商品
        for (Long productId : productQuantityMap.keySet()) {
            cartMapper.deleteByUserIdAndProductId(userId, productId);
        }
    }

    @Override
    public Object findById(Long id) {
        return orderMapper.findById(id);
    }

    @Override
    public void updateStatus(Long orderId, String status) {
        orderMapper.updateStatus(orderId, status);
}

    @Override
    public void delete(Long id) {
        orderMapper.delete(id);
    }
}
